#include "StdAfx.h"
#include "LicenseProxyImpl.h"
#include "LicenseRequestDef.h"

LICENSE_PROXY_NAMESPACE_BEGIN

typedef XmlRequestHandler<tLicenseActiveRequestInfo, LicenseActiveRequestXmlBinding> LicenseActiveRequestHandler;
typedef XmlRequestHandler<tLicenseRequestInfo, LicenseRequestXmlBinding> LicenseRequestHandler;

class LicenseActiveResponseHandler : public XmlResponseHandler<tLicenseActiveResponseInfo, LicenseActiveResponseXmlBinding>
{
public:
	LicenseActiveResponseHandler(ILicenseRequestListener* listener)
	{
		mListener = listener;
	}

	virtual ~LicenseActiveResponseHandler() {}

	virtual void OnRequestFailed(const tErrorInfo* error) 
	{
		if(mListener)
			mListener->OnRequestFailed(Active_License, error);
	}

	virtual void OnRequestSuccess()
	{
		if(mListener)
			mListener->OnRequestSuccess(Active_License, &mResponseInfo.activeCode);
	}

private:
	ILicenseRequestListener* mListener;
};

class LicenseCheckResponseHandler : public XmlResponseHandler<tLicenseCheckResponseInfo, LicenseCheckResponseXmlBinding>
{
public:
	LicenseCheckResponseHandler(ILicenseRequestListener* listener)
	{
		mListener = listener;
	}

	virtual ~LicenseCheckResponseHandler() {}

	virtual void OnRequestFailed(const tErrorInfo* error) 
	{
		if(mListener)
			mListener->OnRequestFailed(Active_License, error);
	}

	virtual void OnRequestSuccess()
	{
		if(mListener)
			mListener->OnRequestSuccess(Check_License, &mResponseInfo.resultCode);
	}

private:
	ILicenseRequestListener* mListener;
};

LicenseProxyImpl::LicenseProxyImpl(void)
{
	m_requestManager = NULL;
	m_listener = NULL;
}

LicenseProxyImpl::~LicenseProxyImpl(void)
{
	if(m_requestManager)
		m_requestManager->Destroy();
	m_listener = NULL;
}

void LicenseProxyImpl::Initialize(const string& serviceURL, const int& port, const bool& encryptData)
{
	m_requestManager = IRequestManager::CreateRequestManager();
	m_requestManager->Initialize(serviceURL, port, encryptData);
}

void LicenseProxyImpl::Destroy()
{
	delete this;
}

void LicenseProxyImpl::SetServiceURL(const string& serviceURL, const int& port)
{
	m_requestManager->SetServiceURL(serviceURL, port);
}

void LicenseProxyImpl::ActiveLicense(const tLicenseInfo& license, const tMachineInfoList& machineInfo)
{
	tLicenseActiveRequestInfo requestInfo;
	requestInfo.object = "license";
	requestInfo.action = "active";
	requestInfo.license = license;
	requestInfo.machineInfo = machineInfo;
	m_requestManager->SendRequest(new LicenseActiveRequestHandler(requestInfo, new LicenseActiveResponseHandler(m_listener)));
}

void LicenseProxyImpl::CheckLicense(const tLicenseInfo& license)
{
	tLicenseRequestInfo requestInfo;
	requestInfo.object = "license";
	requestInfo.action = "check";
	requestInfo.license = license;
	m_requestManager->SendRequest(new LicenseRequestHandler(requestInfo, new LicenseCheckResponseHandler(m_listener)));
}

void LicenseProxyImpl::SetRequestListener(ILicenseRequestListener* listener)
{
	m_listener = listener;
}

ILicenseProxy* ILicenseProxy::CreateLicenseProxy()
{
	return new LicenseProxyImpl;
}

LICENSE_PROXY_NAMESPACE_END